package synology.ui;


import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeListener;

import javax.swing.JDialog;

import synology.SynoBean;

public class SynoBeanDialog extends JDialog
{
	private SynoBeanDialog ref;
	private String oldName;
	private SynoBean bean;
	
	
	private javax.swing.JButton cancelButton;
	private javax.swing.JButton okButton;
	private javax.swing.JComboBox protocolBox;
	private javax.swing.JLabel serverNameLabel;
	private javax.swing.JLabel protocolLabel;
	private javax.swing.JLabel urlLabel;
	private javax.swing.JLabel portLabel;
	private javax.swing.JLabel loginLabel;
	private javax.swing.JLabel passwordLabel;
	private javax.swing.JTextField serverNameField;
	private javax.swing.JTextField loginField;
	private javax.swing.JTextField urlField;
	private javax.swing.JTextField portField;
	private javax.swing.JPasswordField passwordField;


	//--------------------------------------------------------------------------------------------
	SynoBeanDialog()
	{
		ref = this;
		initComponents();
	}


	//--------------------------------------------------------------------------------------------
	SynoBeanDialog(SynoBean bean)
	{
		this();
		initFromBean(bean);
	}

	//--------------------------------------------------------------------------------------------
	public SynoBean getBean()
	{
		return bean;
	}

	//--------------------------------------------------------------------------------------------
	public String getOldName() 
	{
		return oldName;
	}
	
	//--------------------------------------------------------------------------------------------
	private void initFromBean(SynoBean bean)
	{
		oldName = bean.getName();
		serverNameField.setText(bean.getName());
		protocolBox.setSelectedItem(bean.getProtocol());
		urlField.setText(bean.getUrl());
		portField.setText(bean.getPort());
		loginField.setText(bean.getLogin());
		passwordField.setText(bean.getPassword());
	}


	//--------------------------------------------------------------------------------------------
	private void initComponents()
	{
		cancelButton = new javax.swing.JButton();
		okButton = new javax.swing.JButton();
		serverNameField = new javax.swing.JTextField("");
		urlField = new javax.swing.JTextField("");
		portField = new javax.swing.JTextField("");
		protocolBox = new javax.swing.JComboBox();
		loginField = new javax.swing.JTextField("");
		passwordField = new javax.swing.JPasswordField("");
		serverNameLabel = new javax.swing.JLabel();
		protocolLabel = new javax.swing.JLabel();
		urlLabel = new javax.swing.JLabel();
		portLabel = new javax.swing.JLabel();
		loginLabel = new javax.swing.JLabel();
		passwordLabel = new javax.swing.JLabel();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setResizable(false);

		cancelButton.setText("Cancel");
		cancelButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				firePropertyChange("addServer", null, ref);
				setVisible(false);
			}
		});

		okButton.setText("Ok");
		okButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				bean = new SynoBean();
				bean.setName(serverNameField.getText());
				bean.setProtocol((String) protocolBox.getSelectedItem());
				bean.setUrl(urlField.getText());
				bean.setPort(portField.getText());
				bean.setLogin(loginField.getText());
				bean.setPassword(new String(passwordField.getPassword()));

				if(oldName!=null)
					firePropertyChange("serverModified", null, ref);
				else
					firePropertyChange("addServer", null, ref);
				setVisible(false);
			}
		});

		protocolBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "http", "https" }));
		protocolBox.setSelectedIndex(0);

		serverNameLabel.setText("Server Name");
		protocolLabel.setText("Protocol");
		urlLabel.setText("URL");
		portLabel.setText("Port");
		loginLabel.setText("Login");
		passwordLabel.setText("Password");

		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(
				layout.createSequentialGroup().add(
						layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(
								org.jdesktop.layout.GroupLayout.TRAILING,
								layout.createSequentialGroup().addContainerGap(31, Short.MAX_VALUE).add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING).add(serverNameLabel).add(protocolLabel).add(urlLabel).add(portLabel)).add(18, 18, 18).add(
										layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(serverNameField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 123, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE).add(protocolBox, 0, 123, Short.MAX_VALUE).add(urlField,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE).add(portField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE))).add(org.jdesktop.layout.GroupLayout.TRAILING,
								layout.createSequentialGroup().add(74, 74, 74).add(loginLabel).add(18, 18, 18).add(loginField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)).add(org.jdesktop.layout.GroupLayout.TRAILING,
								layout.createSequentialGroup().add(50, 50, 50).add(passwordLabel).add(18, 18, 18).add(passwordField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)).add(
								layout.createSequentialGroup().add(52, 52, 52).add(cancelButton).addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED).add(okButton))).addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING).add(
				layout.createSequentialGroup().addContainerGap().add(
						layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE).add(serverNameField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE).add(serverNameLabel)).addPreferredGap(
						org.jdesktop.layout.LayoutStyle.RELATED).add(
						layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE).add(protocolBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE).add(protocolLabel)).addPreferredGap(
						org.jdesktop.layout.LayoutStyle.RELATED).add(
						layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE).add(urlField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE).add(urlLabel)).addPreferredGap(
						org.jdesktop.layout.LayoutStyle.RELATED).add(
						layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE).add(portField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE).add(portLabel)).addPreferredGap(
						org.jdesktop.layout.LayoutStyle.RELATED).add(
						layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE).add(loginField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE).add(loginLabel)).addPreferredGap(
						org.jdesktop.layout.LayoutStyle.RELATED).add(
						layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE).add(passwordField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE).add(passwordLabel)).add(18, 18, 18).add(
						layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE).add(okButton).add(cancelButton)).addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

		pack();
		setModal(true);

		Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(getGraphicsConfiguration());
		int height = Toolkit.getDefaultToolkit().getScreenSize().height;
		int width = Toolkit.getDefaultToolkit().getScreenSize().width;

		int thisWidth = (width - insets.left - insets.right);
		int thisHeight = (height - insets.bottom - insets.top);

		Dimension dim = getPreferredSize();

		Point p = new Point();
		p.x = (thisWidth - dim.width) / 2;
		p.y = (thisHeight - dim.height) / 2;

		setLocation(p);
	}
}
